package gui.newWindows;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;

import gui.GUISender;
import gui.Main.GUI;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class SetUpWindowPanel extends JPanel
{
    protected GUI main;

    protected JLabel IPAddressLabel;
    protected JLabel PortNumberLabel;
    protected JLabel currentIpInfoLabel;
    protected JLabel connectedHPWLabel;
    protected JTextField IPAddressBox;
    protected JTextField PortNumberBox;
    protected JButton submitButton;

    public SetUpWindowPanel(GUI main)
    {
        // super(new GridBagLayout());

        super(new GridLayout(0, 1));

        this.main = main;

        IPAddressLabel = new JLabel("IP Address   ");
        PortNumberLabel = new JLabel("Port Number");
        currentIpInfoLabel = new JLabel("could not connect to local host");
        try
        {
            currentIpInfoLabel = new JLabel("your Machine:"
                    + InetAddress.getLocalHost().toString());
        }

        catch (UnknownHostException e2)
        {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        connectedHPWLabel = new JLabel("connected to: "
                + gui.Main.GUI.getTargetIpAdress());
        IPAddressBox = new JTextField(12);
        PortNumberBox = new JTextField(12);
        IPAddressBox.setText("192.168.2.");
        PortNumberBox.setText("49200");
        // Build the send button
        submitButton = new JButton("Submit");
        submitButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                submitButtonPressed();
            }

            public void windowClosing(WindowEvent we)
            {
                setSetUpWindowToNull();
            }
        });

        JPanel IPAddressPanel = new JPanel();
        IPAddressPanel.add(IPAddressLabel);
        IPAddressPanel.add(IPAddressBox);
        this.add(IPAddressPanel);

        JPanel PortNumberPanel = new JPanel();
        PortNumberPanel.add(PortNumberLabel);
        PortNumberPanel.add(PortNumberBox);
        this.add(PortNumberPanel);

        JPanel StatusPanel = new JPanel();
        StatusPanel.add(currentIpInfoLabel);
        this.add(StatusPanel);

        JPanel StatusPanel2 = new JPanel();
        StatusPanel.add(connectedHPWLabel);
        this.add(StatusPanel2);

        this.add(submitButton);

    }

    private void setSetUpWindowToNull()
    {
        main.getHyPeerWebDebugger().getStandardCommands()
                .setSetUpWindowToNull();

    }

    private void submitButtonPressed()
    {
        String val1 = "";
        int val2 = -1;
        // main.getHyPeerWebDebugger().getStatus().setContent("");
        try
        {
            val1 = IPAddressBox.getText();
            val2 = Integer.parseInt(PortNumberBox.getText());
        }
        catch (Exception e)
        {
            // main.getHyPeerWebDebugger().getStatus().setContent("Error:  none Integer value found in IPAddress or PortNumber box");
            return;
        }

        main.setTargetIpAdress(val1);
        main.setPortNumber(val2);

        SwingUtilities.getWindowAncestor(this).dispose();

    }
}
